package huaabdelmartin;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

public class ReadAndStoreInput {
	//instance variables
	private HashSet<Car> taxisCollection = new HashSet<Car>();
	private HashSet<Destination> destinationCollection= new HashSet<Destination>();
	private ArrayList<Journey> journeysCollection2015 = new ArrayList<Journey>();
	private TreeSet<String> destinationCollection2014 = new TreeSet<String>();

	//constructor
	public ReadAndStoreInput() {
	}

	//get and set
	public HashSet<Car> getTaxisCollection() {
		return taxisCollection;
	}
	public void setTaxisCollection(HashSet<Car> taxisCollection) {
		this.taxisCollection = taxisCollection;
	}
	public HashSet<Destination> getDestinationCollection() {
		return destinationCollection;
	}
	public void setDestinationCollection(HashSet<Destination> destinationCollection) {
		this.destinationCollection = destinationCollection;
	}
	public ArrayList<Journey> getJourneysCollection2015() {
		return journeysCollection2015;
	}
	public void setJourneysCollection2015(ArrayList<Journey> journeysCollection2015) {
		this.journeysCollection2015 = journeysCollection2015;
	}HashMap<String, String> taxisMap = new HashMap<String, String>();
	public TreeSet<String> getDestinationCollection2014() {
		return destinationCollection2014;
	}
	public void setDestinationCollection2014(
			TreeSet<String> destinationCollection2014) {
		this.destinationCollection2014 = destinationCollection2014;
	}

	/**
	 * Reads in details of cars from taxis.txt file, then stores in taxisCollection instance variable...
	 * @throws IOException
	 * @throws DuplicateDriverIdException 
	 */
	public void readAndStoreTaxisCollection() throws IOException, DuplicateDriverIdException {

		try {
			FileReader fr = new FileReader("src/textFiles/taxis.txt");

			//Scanner gives you useful methods like .next(), readLine()
			Scanner sc = new Scanner(fr); 
			String line = "";
			String vehicleReg = "";
			String  driverFirstName= "";
			String  driverSecondName= "";
			Integer IDincrementer = 0;

			nextTaxi:while ( sc.hasNext() ){ //while there are more lines to be read in
				line = sc.nextLine(); //read line and put into line String
				Scanner fielder = new Scanner(line).useDelimiter(",");//in text file, tokens are separated by commas

				if(fielder.hasNext()){//safer to check that there is another token to be read
					vehicleReg = fielder.next(); //get first field
					vehicleReg = vehicleReg.trim(); //delete white spaces
				}

				if(fielder.hasNext()){
					driverFirstName = fielder.next(); 
					driverFirstName = driverFirstName.trim(); 
				}

				if(fielder.hasNext()){
					driverSecondName = fielder.next(); 
					driverSecondName = driverSecondName.trim(); 
				}

				//Create Driver object using tokens from file and also unique ID, use this Driver to create Car object and add to taxisCollection
				try {

					IDincrementer++;//increment to get new ID
					String driverID = Integer.toString(IDincrementer);

					
						for(Car c : taxisCollection){
							if( c.getDriver().getDriverID().equals(driverID) ){
								throw new DuplicateDriverIdException(driverID);
							}
						}

						Driver driver = new Driver(driverFirstName,driverSecondName,driverID);
						Car car;
						car = new Car(vehicleReg,driver,true,5);
						taxisCollection.add(car); 

				} 
				catch (VehicleRegFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			fr.close();//free resources

			System.out.println("Taxi Collection: "+taxisCollection);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("Sorry file not found....taxis.txt ");
			e.printStackTrace();
		}
	}
	public void readAndStoreDestinationsCollection() throws IOException{
		try{
			FileReader fr = new FileReader("src/textFiles/Destination.txt"); 

			//Scanner gives you useful methods like .next(), readLine()
			Scanner sc = new Scanner(fr); 
			String line = "";
			String destinationName = "";
			String distance = "";

			while ( sc.hasNext() ){ 
				line = sc.nextLine(); 
				Scanner fielder = new Scanner(line).useDelimiter(",");
				destinationName = fielder.next(); //get first field
				destinationName = destinationName.trim(); //delete white spaces
				distance = fielder.next(); //get second field
				distance = distance.trim(); //delete white spaces

				Destination d = new Destination(destinationName, Double.parseDouble(distance));

				destinationCollection.add(d); //put fields into Map
			}

			sc.close();
			fr.close();

			System.out.println("Destinations collection: "+destinationCollection);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("Sorry file not found ... Destination.txt  ");
			e.printStackTrace();
		}
	}

	/**
	 * Reads details of journeys from Journeys2015.txt, stores in journeysCollection2015
	 * 
	 * Read details of journeys from Journeys2015.txt, then for each line loop through all the cars in taxisCollection to check
	 * if the current journey vehicle registration exists, if not then skip current line and continue, do a similar check for destination
	 * 
	 * @throws IOException
	 */
	public void readAndStoreJourneysList2015()throws IOException{
		try{
			FileReader fr = new FileReader("src/textFiles/Journeys2015.txt"); 

			//Scanner gives you useful methods like .next(), readLine()
			Scanner sc = new Scanner(fr); 

			//initialise Strings which will hold tokens from file
			String line = "";
			String TaxiRegNumber = "";
			String destinationName = "";
			String NumberofPassengers = "";

			nextLine: while ( sc.hasNextLine() ){ //while there are more lines to b
				line = sc.nextLine(); //read line and put into line String

				Scanner fielder = new Scanner(line).useDelimiter(",");
				if(fielder.hasNext()){
					TaxiRegNumber = fielder.next(); //get first field
					TaxiRegNumber = TaxiRegNumber.trim(); //delete white spaces
				}

				if(fielder.hasNext()){
					destinationName = fielder.next(); //get second field
					destinationName = destinationName.trim(); //delete white spaces
				}

				if(fielder.hasNext()){
					NumberofPassengers =fielder.next();
					NumberofPassengers = NumberofPassengers.trim();
				}

				Car c = null;//create Car object outside of while loop because you need it inside and outside the loop
				Destination d = null;//create Destinaiton object outside of while loop because you need it inside and outside the loop

				Iterator<Car> iter = taxisCollection.iterator();
				while ( iter.hasNext() ){//loop through Cars collection
					Car car = iter.next();
					if(TaxiRegNumber.equals(car.getRegNumber())){
						c = car;
					}

					//if this is the end of the iteration and a matching car hasn't been found, throw exception then go to the next line of text file
					if (!iter.hasNext() && c == null){
						try {
							throw new MissingFieldException("For journey to "+destinationName+" in car reg "+TaxiRegNumber+" with "+NumberofPassengers+" passengers, unable to find existing car");
						} catch (MissingFieldException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						//if car reg was not found, there is a missing car, so miss this line and go to next line
						continue nextLine;
					}
				}

				Iterator<Destination> iterDest = destinationCollection.iterator();
				while ( iterDest.hasNext() ){//loop through destinations collection

					Destination dest = iterDest.next();
					if(   destinationName.equals( dest.getNameOfDestination() )   ){
						d = dest;
					}
					//if this is the end of the iteration and a matching destination hasn't been found, throw exception then go to the next line of text file
					if (!iterDest.hasNext() && d == null){
						try {
							throw new MissingFieldException("For journey to "+destinationName+" in car reg "+TaxiRegNumber+" with "+NumberofPassengers+" passengers, unable to find existing destination");
						} catch (MissingFieldException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						//if destination was not found, there is a missing destination, so miss this line and go to next line
						continue nextLine;
					}

				}	

				fielder.close();//free up resource
				Journey j = new Journey(2015, c, d, Integer.parseInt(NumberofPassengers) );//create new journey object
				journeysCollection2015.add(j);//add journey object to instance variable
			}

			fr.close();//free up resource
			sc.close();//free up resource

			System.out.println("Journeys 2015: "+journeysCollection2015);

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("Sorry file not found ... Journeys2015.txt");
			e.printStackTrace();
		}
	}

	/**
	 * Reads details of destinations visited 2014 from DestinationsVisited2014.txt, stores in destinationCollection2014
	 * @throws IOException
	 */
	public void readAndStoreDestinationsList2014() throws IOException{

		try{
			FileReader fr = new FileReader("src/textFiles/DestinationsVisited2014.txt"); 
			Scanner sc = new Scanner(fr); 

			//initialise Strings which will hold tokens from file
			String line = "";
			String destinationName = "";

			while ( sc.hasNext() ){ //while there are more lines
				line = sc.nextLine(); //read next line into line variable

				Scanner fielder = new Scanner(line).useDelimiter(",");//get tokens separated by commas
				destinationName = fielder.next(); //got the only token on this line
				destinationName = destinationName.trim(); //remove extra white spaces

				destinationCollection2014.add(destinationName); //add destination to instance variable
			}

			fr.close();//free resources

			System.out.println("Destinations 2014: "+ destinationCollection2014 );
		} catch (FileNotFoundException e) {

			System.out.println("Sorry for not found ... DestinationsVisited2014.txt");
			e.printStackTrace();
		}
	}
}
